package service

import (
	"dbManager/example/dao"
	"dbManager/example/model"
	"log"

	"github.com/zhuxiujia/GoMybatis"
)

type MonitorService struct {
	monitorMapper dao.MonitorMapper
}

func NewMonitorService(monitorMapper dao.MonitorMapper) MonitorService {
	return MonitorService{
		monitorMapper: monitorMapper,
	}
}

// FindAll 查询所有
func (monitorservice MonitorService) FindAll(session GoMybatis.Session) ([]model.Monitor, error) {
	result, err := monitorservice.monitorMapper.FindAll(&session)
	if err != nil {
		return nil, err
	}
	return result, nil
}

// Insert 插入数据
func (monitorservice MonitorService) Insert(session GoMybatis.Session, monitor model.Monitor) bool {
	_, err := monitorservice.monitorMapper.Insert(&session, monitor)
	if err != nil {
		log.Println(err)
		return false
	}
	err = session.Commit()
	if err != nil {
		log.Println(err)
		return false
	}
	return true
}

// FindById 查询一个
func (monitorservice MonitorService) FindById(session GoMybatis.Session, id int) (model.Monitor, error) {
	result, err := monitorservice.monitorMapper.FindById(&session, id)
	if err != nil {
		return model.Monitor{}, err
	}
	return result[0], nil
}

// UpdateById 更新数据(乐观锁版本)
func (monitorservice MonitorService) UpdateById(session GoMybatis.Session, monitor model.Monitor) bool {
	isRollback := false
	monitor_temp, err := monitorservice.FindById(session, monitor.Id)
	if err != nil {
		log.Println(err, "未找到id是", monitor.Id, "的记录")
		return false
	} else {
		version := monitor_temp.Version
		monitor.Version = version
		defer func() {
			err := session.Commit()
			if err != nil {
				log.Println(err)
			}
		}()
		defer func() {
			if e := recover(); e != nil && isRollback == false {
				err := session.Rollback()
				if err != nil {
					log.Println(err)
				}
				isRollback = true
			}
		}()
		sum, err := monitorservice.monitorMapper.UpdateById(&session, monitor)
		if err != nil || sum == 0 {
			log.Println(err)
			err := session.Rollback()
			if err != nil {
				log.Println(err)
			}
			isRollback = true
		}
	}
	if isRollback == true {
		return false
	}
	return true
}

// DeleteByPhysics 物理删除
func (monitorservice MonitorService) DeleteByPhysics(session GoMybatis.Session, id int) bool {
	isRollback := false
	monitor_temp, err := monitorservice.FindById(session, id)
	if err != nil {
		log.Println(err, "未找到id是", id, "的记录")
		return false
	} else {
		version := monitor_temp.Version
		defer func() {
			err := session.Commit()
			if err != nil {
				log.Println(err)
			}
		}()
		defer func() {
			if e := recover(); e != nil && isRollback == false {
				err := session.Rollback()
				if err != nil {
					log.Println(err)
				}
				isRollback = true
			}
		}()
		sum, err := monitorservice.monitorMapper.DeleteByPhysics(&session, id, version)
		if err != nil || sum == 0 {
			log.Println(err)
			err := session.Rollback()
			if err != nil {
				log.Println(err)
			}
			isRollback = true
		}
	}
	if isRollback == true {
		return false
	}
	return true
}

// DeleteByLogic 逻辑删除
func (monitorservice MonitorService) DeleteByLogic(session GoMybatis.Session, id int) bool {
	isRollback := false
	monitor_temp, err := monitorservice.FindById(session, id)
	if err != nil {
		log.Println(err, "未找到id是", id, "的记录")
		return false
	} else {
		version := monitor_temp.Version
		defer func() {
			err := session.Commit()
			if err != nil {
				log.Println(err)
			}
		}()
		defer func() {
			if e := recover(); e != nil && isRollback == false {
				err := session.Rollback()
				if err != nil {
					log.Println(err)
				}
				isRollback = true
			}
		}()
		sum, err := monitorservice.monitorMapper.DeleteByLogic(&session, id, version)
		if err != nil || sum == 0 {
			log.Println(err)
			err := session.Rollback()
			if err != nil {
				log.Println(err)
			}
			isRollback = true
		}
	}
	if isRollback == true {
		return false
	}
	return true
}
func init() {

}
